<?php

class Start extends Controller {

	function Start()
	{
		parent::Controller();
		$this->load->library('form_validation');

	}

	//----------------------Main Controller-------------------\\

	function index()
	{

		$data['main_content'] = 'blank';
		$this->load->view('start_message', $data);
	}

	function cal_volume()
	{
		$data = array();
		if ($query = $this->product_model->get_products()) {
			$data['records'] = $query;
		}

		$data['main_content'] = 'calculate_volume_list';
		$this->load->view('start_message', $data);
	}

	//----------------------ADD FUNCTIONS-------------------\\

	function add_prod()
	{
		$data['main_content'] = 'add_product';
		$this->load->view('start_message', $data);
	}

	function add_cont()
	{
		$this->load->library('formdate');
		$formdate = new FormDate();
		$formdate->setLocale('en_US');
		$formdate->year['start'] = 2009;
		$formdate->year['end'] = 2013;
		$formdate->month['values'] = 'numbers';
		$data['formdate'] = $formdate;
		
		if ($query = $this->product_model->getAllPorts()) {
			foreach ($query as $row) {
				$options[$row->id] = $row->name;
				$data['options'] = $options;
			}
		}
		if ($query = $this->product_model->getAllConveyance()) {
			foreach ($query as $rows) {
				$conveyance[$rows->id] = $rows->name;
				$data['conveyance'] = $conveyance;
			}
		}
		$data['main_content'] = 'add_container';
		$this->load->view('start_message', $data);
	}

	function add_order()
	{
		$this->load->library('formdate');
		$formdate = new FormDate();
		$formdate->setLocale('en_US');
		$formdate->year['start'] = 2009;
		$formdate->year['end'] = 2013;
		$formdate->month['values'] = 'numbers';
		$data['formdate'] = $formdate;


		//page 1
		$data['main_content'] = 'add_order1';
		$this->load->view('start_message', $data);
	}



	function addto_order($id)
	{
		//page with product list
		$data['orderID'] = $id;
		if ($query = $this->product_model->get_products()) {
			$data['records'] = $query;
			$data['main_content'] = 'add_order2';
			$this->load->view('start_message', $data);
		}
	}
	
	function addto_cont(){
	
		$container = $this->uri->segment(3);
		$order = $this->uri->segment(4);
		
	if ($query = $this->product_model->addto_cont($container, $order)) {
			
			$data['grats'] ='Container';
			$data['action'] = 'Added';
			$data['main_content'] = 'grats';
			$this->load->view('start_message', $data);
		}
	}
	
	function addProduct()
	{
		
		$this->form_validation->set_rules("amount", 'amount', 'trim|required');
		
		$id = $this->product_model->getOrderInfo($this->input->post('orderID'));


			if ($query = $this->product_model->addto_order()) {
			$db = $this->product_model->getOrderproduct($this->input->post('orderID'));
				$data['order'] = $db;
				if ($query2 = $this->product_model->get_products()) {
					$data['records'] = $query2;
				}
				$data['orderID'] = $id;
				$data['main_content'] = 'add_order2';
			$this->load->view('start_message', $data);
			}else {
				$this->addto_order($id);
			}
	}
	
	//----------------------DEL FUNCTIONS-------------------\\

	function del_prod()
	{
		$this->product_model->delete_product();

		$data['grats'] ='Products';
		$data['action'] = 'Deleted';
		$data['main_content'] = 'grats';
		$this->load->view('start_message', $data);
	}

	function del_cont()
	{
		$this->product_model->delete_container();

		$data['grats'] ='Container';
		$data['action'] = 'Deleted';
		$data['main_content'] = 'grats';
		$this->load->view('start_message', $data);
	}
	
	function del_order()
	{
		$this->product_model->delete_order();

		$data = array();
		if ($query = $this->product_model->get_orders()) {
			$data['main'] = $query;
		}
		
		$data['grats'] ='Container';
		$data['action'] = 'Deleted';
		$data['main_content'] = 'list_order';
		$this->load->view('start_message', $data);
	}
	
	function del_fromOrder()
	{
		$this->product_model->remove_fromOrder($this->uri->segment(4));
		
	$id = $this->product_model->getOrderInfo($this->uri->segment(3));	
	$db = $this->product_model->getOrderproduct($this->uri->segment(3));
				$data['order'] = $db;
				if ($query2 = $this->product_model->get_products()) {
					$data['records'] = $query2;
				}
				$data['orderID'] = $id;
				$data['main_content'] = 'add_order2';
			$this->load->view('start_message', $data);
	}
	

	//----------------------LIST FUNCTIONS-------------------\\

	function list_prod()
	{
		$data = array();
		if ($query = $this->product_model->get_products()) {
			$data['records'] = $query;
		}

		$data['main_content'] = 'list_product';
		$this->load->view('start_message', $data);
	}
	
	function list_order()
	{
		$data = array();
		if ($query = $this->product_model->get_orders()) {
			$data['main'] = $query;
		}

		$data['main_content'] = 'list_order';
		$this->load->view('start_message', $data);
	}

	
	
	function list_cont()
	{
		$data = array();
		if ($query = $this->product_model->get_containers()) {
			$data['main'] = $query;
		}
		$data['main_content'] = 'list_container';
		$this->load->view('start_message', $data);
	}

	//----------------------EDIT FUNCTIONS-------------------\\

	function edit_cont()
	{
		$data = array();
		if ($query = $this->product_model->get_cont_details($this->uri->segment(3))) {
			$data['records'] = $query;
		}
		if ($query = $this->product_model->getAllPorts()) {
			foreach ($query as $row) {
				$options[$row->id] = $row->name;
				$data['options'] = $options;
			}
		}
		if ($query = $this->product_model->getAllConveyance()) {
			foreach ($query as $rows) {
				$conveyance[$rows->id] = $rows->name;
				$data['conveyance'] = $conveyance;
			}
		}
		$data['main_content'] = 'edit_cont';
		$this->load->view('start_message', $data);
	}

	function edit_prod()
	{
		$data = array();
		if ($query = $this->product_model->get_prod_details($this->uri->segment(3))) {
			$data['records'] = $query;
		}

		$data['main_content'] = 'edit_prod';
		$this->load->view('start_message', $data);
	}

	function edit_product()
	{
		$this->form_validation->set_rules('upc', 'upc', 'trim|required');
		$this->form_validation->set_rules('desc', 'desccription', 'trim|required');
		$this->form_validation->set_rules('model', 'model', 'trim|required');
		$this->form_validation->set_rules('product_MC', 'Product Master Carton', 'trim|required');
		$this->form_validation->set_rules('product_W', 'Product Width', 'trim|required');
		$this->form_validation->set_rules('product_H', 'Product Height', 'trim|required');
		$this->form_validation->set_rules('product_L', 'Product Length', 'trim|required');
		$this->form_validation->set_rules('product_Weight', 'Product Weight', 'trim|required');

		if ($this->form_validation->run() == FALSE) {
			$this->edit_prod();

		}
		else {
			if ($query = $this->product_model->edit_product()) {
				$data['grats'] ='Product';
				$data['action'] = 'Edited';
				$data['main_content'] = 'grats';
				$this->load->view('start_message', $data);
			}else {

				$this->load->view('add_container');
			}
		}
	}

	function edit_container()
	{
		$this->form_validation->set_rules('port', 'port', 'trim|required');
		$this->form_validation->set_rules('date', 'date', 'trim|required');
		$this->form_validation->set_rules('conveyance', 'conveyance name', 'trim|required');
		$this->form_validation->set_rules('container_serial', 'container serial', 'trim|required');
		$this->form_validation->set_rules('bar1', 'bar number', 'trim|required');
		$this->form_validation->set_rules('seal1', 'seal 1', 'trim|required');
		$this->form_validation->set_rules('seal2', 'seal 2', 'trim|required');
		$this->form_validation->set_rules('vessel_name', 'Vessel Name', 'trim|required');
		$this->form_validation->set_rules('ref', 'Dac REF', 'trim|required');
		$this->form_validation->set_rules('booking', 'booking number', 'trim|required');
		$this->form_validation->set_rules('port_unload', 'Port Unloading', 'trim|required');
		$this->form_validation->set_rules('sum_insured', 'Sum Insured', 'trim|required');


		if ($this->form_validation->run() == FALSE) {
			$this->index();
		}
		else {
			if ($query = $this->product_model->edit_container()) {
				$data['grats'] ='Container';
				$data['action'] = 'Edited';
				$data['main_content'] = 'grats';
				$this->load->view('start_message', $data);
			}else {

				$this->load->view('add_container');
			}
		}
	}
	
	function edit_order()
	{//page with product list
		$id = $this->product_model->getOrderInfo($this->uri->segment(3));
		$db = $this->product_model->getOrderproduct($this->uri->segment(3));
		$data['order'] = $db;
		if ($query = $this->product_model->get_products()) {
			$data['records'] = $query;
			$data['orderID'] = $id;
			$data['main_content'] = 'add_order2';
			$this->load->view('start_message', $data);
		}else{
		$this->addto_order($id);
		}
	}
	
	
	


	//----------------------CREATE FUNCTIONS-------------------\\

	function create_cont()
	{
		$this->load->library('form_validation');
		$this->form_validation->set_rules('port', 'port', 'trim|required');
		//$this->form_validation->set_rules('date', 'date', 'trim|required');
		$this->form_validation->set_rules('conveyance', 'conveyance name', 'trim|required');
		$this->form_validation->set_rules('container_serial', 'container serial', 'trim|required');
		$this->form_validation->set_rules('bar1', 'bar number', 'trim|required');
		$this->form_validation->set_rules('seal1', 'seal 1', 'trim|required');
		$this->form_validation->set_rules('seal2', 'seal 2', 'trim|required');
		$this->form_validation->set_rules('vessel_name', 'Vessel Name', 'trim|required');
		$this->form_validation->set_rules('ref', 'Dac REF', 'trim|required');
		$this->form_validation->set_rules('booking', 'booking number', 'trim|required');
		$this->form_validation->set_rules('port_unload', 'Port Unloading', 'trim|required');
		$this->form_validation->set_rules('sum_insured', 'Sum Insured', 'trim|required');


		if ($this->form_validation->run() == FALSE) {
			$this->add_cont();
		}
		else {
			if ($query = $this->product_model->create_container()) {
				$data['grats'] ='Container';
				$data['action'] = 'Created';
				$data['main_content'] = 'grats';
				$this->load->view('start_message', $data);
			}else {

				$this->load->view('add_container');
			}
		}
	}

	function create_prod()
	{
		$this->load->library('form_validation');
		$this->form_validation->set_rules('upc', 'upc', 'trim|required');
		$this->form_validation->set_rules('desc', 'description', 'trim|required');
		$this->form_validation->set_rules('model', 'model', 'trim|required');
		$this->form_validation->set_rules('product_MC', 'Product Master Carton', 'trim|required');
		$this->form_validation->set_rules('product_W', 'Product Width', 'trim|required');
		$this->form_validation->set_rules('product_H', 'Product Height', 'trim|required');
		$this->form_validation->set_rules('product_L', 'Product Length', 'trim|required');
		$this->form_validation->set_rules('product_Weight', 'Product Weight', 'trim|required');

		if ($this->form_validation->run() == FALSE) {
			$this->add_prod();
		}
		else {
			if ($query = $this->product_model->create_product()) {
				$data['grats'] ='Products';
				$data['action'] = 'Added';
				$data['main_content'] = 'grats';
				$this->load->view('start_message', $data);
			}else {

				$this->load->view('add_product');
			}
		}
	}

	function create_order()
	{
		
		$this->form_validation->set_rules('order', 'Order', 'trim|required');

		if ($this->form_validation->run() == FALSE) {
			redirect('start/add_order');
		}
		else {
			if ($query = $this->product_model->create_order()) {
				$id = $this->product_model->getOrderID($this->input->post('order'));
				$this->addto_order($id);
			}else {
				$this->load->view('add_order');
			}
		}
	}
	
	function finalize_order()
	{	
	if ($query = $this->product_model->finalize_order()) {
		$data['orderID'] = $this->input->post('orderID');
		$data['main'] = $this->product_model->get_containersEmpty();
	
				$data['main_content'] = 'addOrderInfo';
				$this->load->view('start_message', $data);
			}else {
				$this->load->view('add_order');
			}
	}
	
	function pdf()
	{
     $this->load->plugin('to_pdf');
	$id = $this->product_model->getPrintInfoID($this->uri->segment(3));
	$data['info']= $id;
	if ($query = $this->product_model->getOrderproduct($this->uri->segment(3))) {
			$data['records'] = $query;
			$data['orderID'] = $id;
			$html = $this->load->view('print', $data, true);
		}
     
     pdf_create($html, 'test');
	}
	
	function print_order()
	{
	$id = $this->product_model->getPrintInfoID($this->uri->segment(3));
	$data['info']= $id;
	if ($query = $this->product_model->getOrderproduct($this->uri->segment(3))) {
			$data['records'] = $query;
			$data['orderID'] = $id;
			$this->load->view('print', $data);
		}else{
		$this->load->view('print', $data);
		}
     
	}
	
}